<?php
class Model_mailer extends Model {
    function Model_mailer() {
        parent::Model();
        $this->fromSession = $this->usersession->currentUser();
    }


    function bringMailer($table, $s_limit, $e_limit, $sort, $dir, $query) {


		if($sort == "") $sort = "mailer.name";
        $this->db->order_by($sort, $dir);
        $this->db->select("mailer.mailer_id, mailer.name ,mailer.client_id,client.name as client_name,mailer_task_status, CONCAT(client.name , ' -  ', mailer.name ) as mailer_client", false);
        $this->db->join(" client ", "client.client_id=mailer.client_id", "left");
        $this->db->join("mailer_task ", "mailer_task.mailer_id=mailer.mailer_id and mailer_task_status !=10", "left");

        if ($query != "") {
            $this->db->like(" mailer.name ", $query);
        }
		//$this->db->where("client.client_id",1063);
        $this->db->limit($e_limit, $s_limit);
        $this->db->group_by("mailer.mailer_id");
        $query = $this->db->get($table);
        $result = $query->result_array();
        $totalCount = $this->db->count_all_results("mailer");

        $data = array(
            'totalCount' => $totalCount,
            'DS_MAILER' => $result
        );
        return $data;
    }


    function addMailer($data) {

        $this->db->where("client_id", $data["client_id"]);
        $this->db->where("lower(name)", strLower($data["name"]));
        $iFound = $this->db->get("mailer")->num_rows();

        if ($iFound > 0) {
            return false;
        }

        $dataInsert = array(
            "name" => strTitle($data["name"]),
            "client_id" => $data["client_id"],
            "body" => "LOWER_HEADER , TITLE_HEADER ,  NAME,  _HEADER , _DISCLAIMER , _COMPANY,  _ADDRESS, _PHONE, _MOBIL,  _WEB, _CUI, _DOMAIN, _SUBDOMAIN, _EMAIL1, _EMAIL2, _EMPLOYEE, _POSITION,  _SUBPOSITION, _COUNTY , _CITY",
            "create_by" => $this->fromSession["user_id"]
        );
        $this->db->insert('mailer', $dataInsert);
        return true;
    }


    function deleteMailer($data) {
        $this->db->where("mailer_id", $data['idMailer']);
        $this->db->delete("mailer");
    }


    function getMailerSettings($idMailer) {
        $this->db->select("mailer.*, D.name as test_domain_val , SD.name as test_subdomain_val , P.name as test_position_val , SP.name as test_subposition_val, county.name as test_county_val, city.name as test_city_val " ) ;
        $this->db->join("domain D","D.domain_id=mailer.test_domain","left");
        $this->db->join("domain SD","SD.domain_id=mailer.test_subdomain","left");
        $this->db->join("position P","P.position_id=mailer.test_position","left");
        $this->db->join("position SP","SP.position_id=mailer.test_subposition","left");
        $this->db->join("county","county.county_id=mailer.test_county","left");
        $this->db->join("city","city.city_id=mailer.test_city","left");
        $query = $this->db->get_where("mailer", array('mailer_id' => $idMailer));
        return $query->row();
    }


    function saveMailerSettings($data) {
        $iMailerId = $data["mailer_id"];

        /*foreach($data as $key=>$value) {
            $string.=" $key='".$value."' ,";
        }
        
        $query="update mailer1 set ".substr($string,0,-1)." where mailer_id={$iMailerId}";
        $this->db->query($query);*/

        $this->db->where("mailer_id", $iMailerId);
        $this->db->set($data);
        $this->db->update("mailer");
    }


    /**** ATASAMENTE ***/
    function bringAttach($table, $idMailer, $sType, $s_limit, $e_limit, $sort, $dir) {

        $this->db->order_by($sort, $dir);
        $this->db->limit($e_limit, $s_limit);
        $this->db->where("mailer_id", $idMailer);
        $this->db->where("type", $sType);
        $query = $this->db->get($table);

        $totalCount = $this->db->count_all_results();
        $data = $query->result_array();

        $data = array(
            'totalCount_' . strtolower($sType) => $totalCount,
            'DS_' . strtoupper($sType) => $data
        );
        return $data;
    }


    function deleteAttach($data) {

        $iAttachId = $data["id"];


        $this->db->where("id", $iAttachId);
        $query = $this->db->get("mailer_attach");

        if ($query->num_rows() != 1) {
            return;
        }

        $result = $query->row_array();

        $path_full = $this->config->item('upload_path_full');
        $path_thumb = $this->config->item('upload_path_thumb');
        $path_attach = $this->config->item('upload_path_attach');

        if ($result["type"] == "image") {
            @unlink($path_full . $result["name"]);
            @unlink($path_thumb . $result["name"]);

            $this->db->where("id", $iAttachId);
            $this->db->delete("mailer_attach");
        }
        if ($result["type"] == "attach") {
            @unlink($path_attach . $result["name"]);

            $this->db->where("id", $iAttachId);
            $this->db->delete("mailer_attach");
        }

    }

    function addAttach($file_name, $mailer_id, $type) {
        $data_insert = array(
            "mailer_id" => $mailer_id,
            "type" => $type,
            'name' => $file_name);
        $query = $this->db->insert('mailer_attach', $data_insert);
        return $query;
    }



    /**** LINKURI ***/
    function bringLink($table, $idMailer, $s_limit, $e_limit, $sort, $dir) {

        $this->db->order_by($sort, $dir);
        $this->db->limit($e_limit, $s_limit);
        $this->db->where("mailer_id", $idMailer);
        $query = $this->db->get($table);

        $totalCount = $this->db->count_all_results();
        $data = $query->result_array();

        $data = array(
            'totalCount_link'  => $totalCount,
            'DS_LINK'  => $data
        );
        return $data;
    }


	 /*
     * Adaugare / editare
     * @param  $aData
     * @return bool|string
     */
    function operationLink($aData) {
        $aTemp = array(
            "link_nume" => $aData["link_nume"] ,
	        "mailer_id" =>$aData["mailer_id"]
        );
        //insert
        if ($aData["link_id"] == 0) {
            $this->db->insert("mailer_link", $aTemp);
	        $id = $this->db->insert_id();

        }
        else {
	        $id = $aData["link_id"];
            $this->db->where("link_id", $aData["link_id"]);
            $this->db->update("mailer_link", $aTemp);
        }

	    $this->db->where("link_id",$id);
	    $this->db->set("link_cod","LINKX".$id);
	    $this->db->update("mailer_link")   ;

        return TRUE;
    }


	function getLink($aData) {
        $this->db->where("link_id", $aData["iRec"]);
        return $this->db->get("mailer_link")->row_array();
	}


    function deleteLink($data) {
        $this->db->where("link_id", $data['id']);
        $this->db->delete("mailer_link");
    }
}//end model